using System;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ScottCateAjax2008.Lesson07
{
    public delegate string MathOperation( int a, int b );

    public enum MathOperator : int
    {
        ShowGrid = 0,
        Add = 1,
        Subtract = 2,
        Multiply = 3,
        Divide = 4
    }

    public partial class C_ServerDelegates : Page
    {
        protected void Page_Load ( object sender, EventArgs e )
        {
            if(!Page.IsPostBack)
                BuildMatrix(ShowGrid);
        }

        private void BuildMatrix(MathOperation doMath)
        {
            MatrixPlaceHolder.Controls.Clear();
            Table table = new Table();

            for (int a = 0; a <= 10; a++)
            {
                TableRow row = new TableRow();
                for (int b = 0; b <= 10; b++)
                {
                    TableCell cell = GetCell();
                    cell.Text = doMath( a, b );
                    row.Cells.Add( cell );
                }
                table.Rows.Add( row );
            }

            MatrixPlaceHolder.Controls.Add(table);
        }

        private static TableCell GetCell()
        {
            TableCell cell = new TableCell();
            cell.BorderWidth = new Unit(1);
            cell.BorderColor = Color.Black;
            return cell;
        }

        protected void MathOperatorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedValue = Convert.ToInt32( this.MathOperatorDropDownList.SelectedValue );
            MathOperator math = (MathOperator)selectedValue;
            switch (math)
            {
                case MathOperator.ShowGrid:
                    BuildMatrix(ShowGrid);
                    break;
                case MathOperator.Add :
                    BuildMatrix(Add);
                    break;
                case MathOperator.Subtract:
                    BuildMatrix(Subtract);
                    break;
                case MathOperator.Multiply:
                    BuildMatrix(Multiply);
                    break;
                case MathOperator.Divide:
                    BuildMatrix(Divide);
                    break;
                default:
                    break;
            }
        }

        private static string ShowGrid(int a, int b)
        {
            return string.Format( "{0},{1}", a, b );
        }

        private static string Add(int a, int b)
        {
            return ( a + b ).ToString();
        }

        private static string Subtract(int a, int b)
        {
            return ( a - b ).ToString();
        }

        private static string Multiply(int a, int b)
        {
            return ( a*b ).ToString();
        }

        private static string Divide(int a, int b)
        {
            if (b == 0) return "err";
            return ( (float)a/b ).ToString();
        }
    }
}









